package com.jerei.plugin.dao.impl;

import com.jerei.plugin.dao.IWcmCommonAdminuserDao;
import com.jerei.plugin.entity.WcmCommonAdminuser;
import com.jerei.plugin.utils.JrJdbcTemplate;
import com.jerei.utils.condition.ConditionEntity;
import com.jerei.utils.condition.ConditionTools;
import com.jerei.utils.condition.SQLEntity;
import com.jerei.utils.spring.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by sunfeihu on 2016/10/20.
 */
@Repository
public class WcmCommonAdminuserDaoImpl implements IWcmCommonAdminuserDao {

    @Autowired
    private JrJdbcTemplate jrJdbcTemplate;

    @Override
    public WcmCommonAdminuser save(WcmCommonAdminuser entity) {
        final String sql = " insert into wcm_common_adminuser (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,usern,password,name,sex,company,dept,telephone,email,state,login_count,last_login_date,last_login_ip,allow_ip,allow_time,menu_no,gid,area_level_id,city_level_id,login_fail_count) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        final WcmCommonAdminuser finalEntity = entity;
        this.jrJdbcTemplate.getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setObject(1, finalEntity.getUuid());
                if (null != finalEntity.getAddDate()) {
                    ps.setObject(2, new java.sql.Timestamp(finalEntity.getAddDate().getTime()));
                } else {
                    ps.setObject(2, null);
                }
                ps.setObject(3, finalEntity.getAddIp());
                ps.setObject(4, finalEntity.getAddUser());
                if (null != finalEntity.getUpdateDate()) {
                    ps.setObject(5, new java.sql.Timestamp(finalEntity.getUpdateDate().getTime()));
                } else {
                    ps.setObject(5, null);
                }
                ps.setObject(6, finalEntity.getUpdateIp());
                ps.setObject(7, finalEntity.getUpdateUser());
                ps.setObject(8, finalEntity.getUsern());
                ps.setObject(9, finalEntity.getPassword());
                ps.setObject(10, finalEntity.getName());
                ps.setObject(11, finalEntity.getSex());
                ps.setObject(12, finalEntity.getCompany());
                ps.setObject(13, finalEntity.getDept());
                ps.setObject(14, finalEntity.getTelephone());
                ps.setObject(15, finalEntity.getEmail());
                ps.setObject(16, finalEntity.getState());
                ps.setObject(17, finalEntity.getLoginCount());
                if (null != finalEntity.getLastLoginDate()) {
                    ps.setObject(18, new java.sql.Timestamp(finalEntity.getLastLoginDate().getTime()));
                } else {
                    ps.setObject(18, null);
                }
                ps.setObject(19, finalEntity.getLastLoginIp());
                ps.setObject(20, finalEntity.getAllowIp());
                ps.setObject(21, finalEntity.getAllowTime());
                ps.setObject(22, finalEntity.getMenuNo());
                ps.setObject(23, finalEntity.getGid());
                ps.setObject(24, finalEntity.getAreaLevelId());
                ps.setObject(25, finalEntity.getCityLevelId());
                ps.setObject(26, finalEntity.getLoginFailCount());
                return ps;
            }
        }, keyHolder);
        Long id = keyHolder.getKey().longValue();
        return this.find(id);
    }

    @Override
    public int delete(Long id) {
        final String sql = " delete from wcm_common_adminuser where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                id
        });
    }

    @Override
    public int update(WcmCommonAdminuser entity) {
        final String sql = " update wcm_common_adminuser set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,usern=?,password=?,name=?,sex=?,company=?,dept=?,telephone=?,email=?,state=?,login_count=?,last_login_date=?,last_login_ip=?,allow_ip=?,allow_time=?,menu_no=?,gid=?,area_level_id=?,city_level_id=?,login_fail_count=? where id = ?";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                entity.getUuid(), entity.getAddDate(), entity.getAddIp(), entity.getAddUser(), entity.getUpdateDate(), entity.getUpdateIp(), entity.getUpdateUser(), entity.getUsern(), entity.getPassword(), entity.getName(), entity.getSex(), entity.getCompany(), entity.getDept(), entity.getTelephone(), entity.getEmail(), entity.getState(), entity.getLoginCount(), entity.getLastLoginDate(), entity.getLastLoginIp(), entity.getAllowIp(), entity.getAllowTime(), entity.getMenuNo(), entity.getGid(), entity.getAreaLevelId(), entity.getCityLevelId(), entity.getLoginFailCount(),
                entity.getId()
        });
    }

    @Override
    public WcmCommonAdminuser find(Long id) {
        final String sql = " select * from wcm_common_adminuser where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().queryForObject(sql, new Object[]{id}, new WcmCommonAdminuserRowerMapper());
    }

    @Override
    public void save(List<WcmCommonAdminuser> list) {
        if (null != list && list.size() > 0) {
            final String sql = " insert into wcm_common_adminuser (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,usern,password,name,sex,company,dept,telephone,email,state,login_count,last_login_date,last_login_ip,allow_ip,allow_time,menu_no,gid,area_level_id,city_level_id,login_fail_count) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonAdminuser entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[26];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getUsern();
                objArr[8] = entity.getPassword();
                objArr[9] = entity.getName();
                objArr[10] = entity.getSex();
                objArr[11] = entity.getCompany();
                objArr[12] = entity.getDept();
                objArr[13] = entity.getTelephone();
                objArr[14] = entity.getEmail();
                objArr[15] = entity.getState();
                objArr[16] = entity.getLoginCount();
                objArr[17] = entity.getLastLoginDate();
                objArr[18] = entity.getLastLoginIp();
                objArr[19] = entity.getAllowIp();
                objArr[20] = entity.getAllowTime();
                objArr[21] = entity.getMenuNo();
                objArr[22] = entity.getGid();
                objArr[23] = entity.getAreaLevelId();
                objArr[24] = entity.getCityLevelId();
                objArr[25] = entity.getLoginFailCount();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void delete(List<Long> ids) {
        if (null != ids && ids.size() > 0) {
            final String sql = " delete from wcm_common_adminuser where id = ? ";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            for (int i = 0; i < ids.size(); i++) {
                objArr = new Object[1];
                objArr[0] = ids.get(i);
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void update(List<WcmCommonAdminuser> list) {
        if (null != list && list.size() > 0) {
            final String sql = " update wcm_common_adminuser set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,usern=?,password=?,name=?,sex=?,company=?,dept=?,telephone=?,email=?,state=?,login_count=?,last_login_date=?,last_login_ip=?,allow_ip=?,allow_time=?,menu_no=?,gid=?,area_level_id=?,city_level_id=?,login_fail_count=? where id = ?";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonAdminuser entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[27];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getUsern();
                objArr[8] = entity.getPassword();
                objArr[9] = entity.getName();
                objArr[10] = entity.getSex();
                objArr[11] = entity.getCompany();
                objArr[12] = entity.getDept();
                objArr[13] = entity.getTelephone();
                objArr[14] = entity.getEmail();
                objArr[15] = entity.getState();
                objArr[16] = entity.getLoginCount();
                objArr[17] = entity.getLastLoginDate();
                objArr[18] = entity.getLastLoginIp();
                objArr[19] = entity.getAllowIp();
                objArr[20] = entity.getAllowTime();
                objArr[21] = entity.getMenuNo();
                objArr[22] = entity.getGid();
                objArr[23] = entity.getAreaLevelId();
                objArr[24] = entity.getCityLevelId();
                objArr[25] = entity.getLoginFailCount();
                objArr[26] = entity.getId();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public PageBean<WcmCommonAdminuser> page(int nowPage, int pageSize, ConditionEntity conditionEntity) {
        SQLEntity sqlEntity = ConditionTools.conditionEntities2SQL(conditionEntity);
        final String sql = " select * from wcm_common_adminuser where 1 = 1 " + sqlEntity.getSql() + " order by " + sqlEntity.getOrder();
        return new PageBean<WcmCommonAdminuser>(sql, sqlEntity.getParams(), nowPage, pageSize, new WcmCommonAdminuserRowerMapper(), this.jrJdbcTemplate.getJdbcTemplate());
    }

    @Override
    public WcmCommonAdminuser getAdminuserByUsern(String usern) {
        final String sql = " select * from wcm_common_adminuser where usern = ? ";
        WcmCommonAdminuser adminuser = this.jrJdbcTemplate.getJdbcTemplate().queryForObject(sql, new WcmCommonAdminuserRowerMapper(), new Object[]{usern});
        return adminuser;
    }


}


class WcmCommonAdminuserRowerMapper implements RowMapper<WcmCommonAdminuser> {
    @Override
    public WcmCommonAdminuser mapRow(ResultSet rs, int rowNum) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        Set<String> fieldSet = new HashSet<String>();
        for (int i = 1; i <= columnCount; i++) {
            fieldSet.add(rsmd.getColumnName(i));
        }
        WcmCommonAdminuser entity = new WcmCommonAdminuser();
        if (fieldSet.contains("id") && null != rs.getObject("id")) {
            entity.setId(rs.getLong("id"));
        }
        if (fieldSet.contains("uuid") && null != rs.getObject("uuid")) {
            entity.setUuid(rs.getString("uuid"));
        }
        if (fieldSet.contains("add_date") && null != rs.getObject("add_date")) {
            entity.setAddDate(new java.util.Date(rs.getTimestamp("add_date").getTime()));
        }
        if (fieldSet.contains("add_ip") && null != rs.getObject("add_ip")) {
            entity.setAddIp(rs.getString("add_ip"));
        }
        if (fieldSet.contains("add_user") && null != rs.getObject("add_user")) {
            entity.setAddUser(rs.getString("add_user"));
        }
        if (fieldSet.contains("update_date") && null != rs.getObject("update_date")) {
            entity.setUpdateDate(new java.util.Date(rs.getTimestamp("update_date").getTime()));
        }
        if (fieldSet.contains("update_ip") && null != rs.getObject("update_ip")) {
            entity.setUpdateIp(rs.getString("update_ip"));
        }
        if (fieldSet.contains("update_user") && null != rs.getObject("update_user")) {
            entity.setUpdateUser(rs.getString("update_user"));
        }
        if (fieldSet.contains("usern") && null != rs.getObject("usern")) {
            entity.setUsern(rs.getString("usern"));
        }
        if (fieldSet.contains("password") && null != rs.getObject("password")) {
            entity.setPassword(rs.getString("password"));
        }
        if (fieldSet.contains("name") && null != rs.getObject("name")) {
            entity.setName(rs.getString("name"));
        }
        if (fieldSet.contains("sex") && null != rs.getObject("sex")) {
            entity.setSex(rs.getInt("sex"));
        }
        if (fieldSet.contains("company") && null != rs.getObject("company")) {
            entity.setCompany(rs.getString("company"));
        }
        if (fieldSet.contains("dept") && null != rs.getObject("dept")) {
            entity.setDept(rs.getString("dept"));
        }
        if (fieldSet.contains("telephone") && null != rs.getObject("telephone")) {
            entity.setTelephone(rs.getString("telephone"));
        }
        if (fieldSet.contains("email") && null != rs.getObject("email")) {
            entity.setEmail(rs.getString("email"));
        }
        if (fieldSet.contains("state") && null != rs.getObject("state")) {
            entity.setState(rs.getLong("state"));
        }
        if (fieldSet.contains("login_count") && null != rs.getObject("login_count")) {
            entity.setLoginCount(rs.getInt("login_count"));
        }
        if (fieldSet.contains("last_login_date") && null != rs.getObject("last_login_date")) {
            entity.setLastLoginDate(new java.util.Date(rs.getTimestamp("last_login_date").getTime()));
        }
        if (fieldSet.contains("last_login_ip") && null != rs.getObject("last_login_ip")) {
            entity.setLastLoginIp(rs.getString("last_login_ip"));
        }
        if (fieldSet.contains("allow_ip") && null != rs.getObject("allow_ip")) {
            entity.setAllowIp(rs.getString("allow_ip"));
        }
        if (fieldSet.contains("allow_time") && null != rs.getObject("allow_time")) {
            entity.setAllowTime(rs.getString("allow_time"));
        }
        if (fieldSet.contains("menu_no") && null != rs.getObject("menu_no")) {
            entity.setMenuNo(rs.getLong("menu_no"));
        }
        if (fieldSet.contains("gid") && null != rs.getObject("gid")) {
            entity.setGid(rs.getLong("gid"));
        }
        if (fieldSet.contains("area_level_id") && null != rs.getObject("area_level_id")) {
            entity.setAreaLevelId(rs.getLong("area_level_id"));
        }
        if (fieldSet.contains("city_level_id") && null != rs.getObject("city_level_id")) {
            entity.setCityLevelId(rs.getLong("city_level_id"));
        }
        if (fieldSet.contains("login_fail_count") && null != rs.getObject("login_fail_count")) {
            entity.setLoginFailCount(rs.getInt("login_fail_count"));
        }
        return entity;
    }
}